﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Denghi_u_Denghiguidi : System.Web.UI.UserControl
{
    #region[Khai bao]
    string p = "";
    string uID = "";
    static string adm = "";
    #endregion
    #region[Page]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        if (Session["p"] != null) { p = Session["p"].ToString(); }
        if (Session["adm"] != null) { adm = Session["adm"].ToString(); }
        if (!IsPostBack) { Viewlist(); }
    }
    #endregion
    #region[Viewlist]
    void Viewlist()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistAsc]
    void ViewlistAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNameAsc]
    void ViewlistNameAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Name_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Name_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNameDesc]
    void ViewlistNameDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Name_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Name_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistStatusAsc]
    void ViewlistStatusAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Status_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Status_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistStatusDesc]
    void ViewlistStatusDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Status_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Status_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNguoinhanAsc]
    void ViewlistNguoinhanAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNguoinhanDesc]
    void ViewlistNguoinhanDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[View, Delete]
    protected void rptDanhsach_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string ten = e.CommandName.ToString();
        Session["ID"] = e.CommandArgument.ToString();
        switch (ten)
        {
            case "View":
                List<tbProposalDATA> list = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
                lblNguoinhan.Text = list[0].Nguoinhan;
                lblTieude.Text = list[0].prName;
                lblIDnguoigui.Text = list[0].uRec;
                if (list[0].prStatus == "1") { lbtPhanhoi.Visible = false; } else { lbtPhanhoi.Visible = true; }
                List<tbUsersDATA> listu = tbUserDB.tbUsers_GetByID(list[0].uRec);
                if (listu.Count > 0)
                {
                    lblPhongban.Text = listu[0].dpName;
                    lblChucvu.Text = listu[0].psName;
                    lblTell.Text = listu[0].uTel;
                    lblEmail.Text = listu[0].uEmail;
                }
                listu.Clear();
                listu = null;
                List<tbReplyDATA> listre = tbReplyDB.tbReply_GetByIDprID(Session["ID"].ToString());
                rptSub.DataSource = listre;
                rptSub.DataBind();
                listre.Clear();
                listre = null;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                pnPhanhoi.Visible = false;
                lbtGuiPhanhoi.Text = "Gửi phản hồi";
                lblEdit.Text = "0";
                list.Clear();
                list = null;
                break;
            case "Edit":
                List<tbProposalDATA> listedit = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
                if (listedit.Count > 0)
                {
                    lblEdit.Text = "1";
                    fckNoidung.Value = listedit[0].prContent;
                    ltrFile.Text = listedit[0].prFiles;
                    lbtGuiPhanhoi.Text = "Cập nhật";
                    pnDanhsach.Visible = false;
                    pnUpdate.Visible = false;
                    pnPhanhoi.Visible = true;
                }
                break;
            case "Delete":
                tbProposalDB.tbProposal_Delete(Session["ID"].ToString());
                lblThongbao.Text = "Xóa thành công thành viên !!";
                Viewlist();
                break;
        }
    }
    #endregion
    #region[Delete]
    protected void Delete(object sender, System.EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return confirm('Bạn chắc chắn muốn xóa thông tin vừa chọn !!') ";
    }
    #endregion
    #region[FormatDate]
    protected string FormatDate(object date)
    {
        if (date.ToString().Trim().Length > 0)
        {
            if (DateTime.Parse(date.ToString()).Year != 1900)
            {
                DateTime dNgay = Convert.ToDateTime(date.ToString());
                return ((DateTime)dNgay).ToString("dd/MM/yyyy hh:mm");
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }
    #endregion
    #region[ViewStatus]
    protected string Status(string status)
    {
        string Chuoi = "";
        if (status == "0") { Chuoi = "Chưa duyệt"; } else { Chuoi = "Đã duyệt"; }
        return Chuoi;
    }
    #endregion
    #region[Phanhoi]
    protected string Phanhoi(string prID)
    {
        string Chuoi = "";
        List<tbReplyDATA> list = tbReplyDB.tbReply_GetBy_prID_uRec(uID, prID);
        Chuoi = list.Count.ToString();
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
    #region[Viewnguoinhan]
    protected string Viewnguoinhan(string sendid)
    {
        string Chuoi = "";
        string Chuoi1 = "<img src=\"images/admin/kt.gif\" style=\"border: 0px\"/>";
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByNguoiNhan_prID(sendid);
        if (list.Count > 0)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 1)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 2)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 3)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 4)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 5)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 6)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 7)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 8)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 9)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 10)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else
                {
                    Chuoi = Chuoi1 + "<font style=\"color: #0033ff\">" + list[i].uName + "</font>";
                }
            }
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
    #region[SortDesc]
    protected void lbtGiam_Click(object sender, EventArgs e)
    {
        Viewlist();
    }
    #endregion
    #region[SortAsc]
    protected void lbtTang_Click(object sender, EventArgs e)
    {
        ViewlistAsc();
    }
    #endregion
    #region[SortNameAsc]
    protected void ltrTieudetangdan_Click(object sender, EventArgs e)
    {
        ViewlistNameAsc();
    }
    #endregion
    #region[SortNameDesc]
    protected void lbtTieudegiamdan_Click(object sender, EventArgs e)
    {
        ViewlistNameDesc();
    }
    #endregion
    #region[SortStatusAsc]
    protected void lbtTinhtrangtang_Click(object sender, EventArgs e)
    {
        ViewlistStatusAsc();
    }
    #endregion
    #region[SortStatusDesc]
    protected void lbtTinhtranggiam_Click(object sender, EventArgs e)
    {
        ViewlistStatusDesc();
    }
    #endregion
    #region[SortNguoinhanAsc]
    protected void  lbtNguoinhantang_Click(object sender, EventArgs e)
    {
        ViewlistNguoinhanAsc();
    }
    #endregion
    #region[SortNguoinhanGiam]
    protected void  lbtNguoinhangiam_Click(object sender, EventArgs e)
    {
        ViewlistNguoinhanDesc();
    }
    #endregion
    #region[Back]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {
        pnDanhsach.Visible = true;
        pnUpdate.Visible = false;
        Viewlist();
    }
    #endregion
    #region[Capnhatphanhoi]
    protected void lbtGuiPhanhoi_Click(object sender, EventArgs e)
    {
        if (lblEdit.Text == "0")
        {
            if (fckNoidung.Value == "" || fckNoidung.Value == "<br type=\"_moz\" />") { lblLoi.Text = "Vui lòng nhập nội dung phản hồi !!"; return; } else { lblLoi.Text = ""; }
            string img_path = "";
            if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
            {
                img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
                fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
                img_path = "Users/files/" + img_path.ToString().Trim();
            }
            tbReplyDATA ObjtbReplyDATA = new tbReplyDATA();
            ObjtbReplyDATA.prID = Session["ID"].ToString();
            ObjtbReplyDATA.reName = lblTieude.Text;
            ObjtbReplyDATA.reContent = fckNoidung.Value;
            ObjtbReplyDATA.peFiles = img_path;
            ObjtbReplyDATA.uSend = uID;
            ObjtbReplyDATA.uRec = lblIDnguoigui.Text;
            ObjtbReplyDATA.reType = "0";
            try
            {
                if (tbReplyDB.tbReply_Add(ObjtbReplyDATA))
                {
                    lblThongbao.Text = "Phản hồi đề nghị thành công !!";
                    Resetcontrol();
                    pnDanhsach.Visible = true;
                    pnUpdate.Visible = false;
                    pnPhanhoi.Visible = false;
                }
            }
            catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
            Session["ID"] = null;
        }
        else
        {
            string img_path = "";
            if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
            {
                img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
                fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
                img_path = "Users/files/" + img_path.ToString().Trim();
            }
            List<tbProposalDATA> list = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
            if (list.Count > 0)
            {
                list[0].prID = list[0].prID;
                list[0].prContent = fckNoidung.Value;
                list[0].prFiles = img_path;
                if (tbProposalDB.tbProposal_Update_New(list[0]))
                {
                    lblThongbao.Text = "Cập nhật đề nghị thành công !!";
                    Resetcontrol();
                    pnDanhsach.Visible = true;
                    pnUpdate.Visible = false;
                    pnPhanhoi.Visible = false;
                }
            }
        }
    }
    #endregion
    #region[Huy]
    protected void lbtHuy_Click(object sender, EventArgs e)
    {
        pnPhanhoi.Visible = false;
    }
    #endregion
    #region[ViewClass]
    protected string ViewClass(string dk)
    {
        string Chuoi = "";
        if (dk == "0")
        {
            Chuoi = "icodi";
        }
        else
        {
            Chuoi = "icoden";
        }
        return Chuoi;
    }
    #endregion
    #region[Resetcontrol]
    void Resetcontrol()
    {
        lblTieude.Text = "";
        lblPhongban.Text = "";
        lblChucvu.Text = "";
        lblTell.Text = "";
        lblEmail.Text = "";
        fckNoidung.Value = "";
    }
    #endregion
    #region[Phanhoi]
    protected void lbtPhanhoi_Click(object sender, EventArgs e)
    {
        pnPhanhoi.Visible = true;
    }
    #endregion
    #region[ViewNameuSend]
    protected string NameUser(string uid)
    {
        string Chuoi = "";
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(uid);
        if (list.Count > 0)
        {
            Chuoi = list[0].uName;
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
}
